package com.example.websocket.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
@ServerEndpoint(value = "/websocket")
public class PureWebSocket {
    private Session session;

    private CopyOnWriteArraySet<PureWebSocket> webSocketSet = new CopyOnWriteArraySet<>();

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        this.webSocketSet.add(this);
        System.out.println("OnOpen...");
    }

    @OnClose
    public void onClose() {
        this.webSocketSet.remove(this);
        System.out.println("onClose...");
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("onMessage...");
    }

    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("onError...");
    }
}
